/**
 * This file is released under the GNU General Public License.
 * Refer to the COPYING file distributed with this package.
 *
 * Copyright (c) 2008-2009 WURFL-Pro srl
 */
package net.sourceforge.wurfl.browser.server;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sourceforge.wurfl.core.CapabilityNotDefinedException;
import net.sourceforge.wurfl.core.Device;
import net.sourceforge.wurfl.core.MarkUp;
import net.sourceforge.wurfl.core.WURFLHolder;
import net.sourceforge.wurfl.core.WURFLManager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 * @author Michael Maguire
 */
public class GetIsMobile extends HttpServlet {

	/** Serial */
	private static final long serialVersionUID = 11L;

	/** Logger */
	private final Log log = LogFactory.getLog(getClass());

	private static final String USER_AGENT = "User-Agent";
	private static final String ORIGINAL_USER_AGENT = "X-Original-User-Agent";

	/**
	 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
	 * methods.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 */
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		WURFLHolder wurflHolder = (WURFLHolder) getServletContext()
				.getAttribute("net.sourceforge.wurfl.core.WURFLHolder");

		WURFLManager wurfl = wurflHolder.getWURFLManager();

		// We're not interested in the correct device for this request, but
		// instead
		// the one that is passed as the x-original-user-agent header.
		// Device device = wurfl.getDeviceForRequest(request);

		String originalUserAgent = request.getHeader(ORIGINAL_USER_AGENT);
		if (null == originalUserAgent) {
			// Fine, for debugging, if that header wasn't specified,
			// just perform the answer on the user-agent header.
			originalUserAgent = request.getHeader(USER_AGENT);
		}

		Device device = wurfl.getDeviceForRequest(originalUserAgent);

		log.debug("Device: " + device.getId());
		log.debug("Capability: " + device.getCapability("preferred_markup"));

		MarkUp markUp = device.getMarkUp();
		
		String mobile = device.getCapability("is_wireless_device");

		request.setAttribute("markUp", markUp);
		request.setAttribute("device", device);

		// To allow caller to know the answer without needed to parse the response,
		// we use the (still OK because it's in 2XX range) status code 203 instead of 200.
		if( "true".equals(mobile))
		{
			response.setStatus(203);
		}
		else
		{
			response.setStatus(200);
		}
		
		OutputStream os = response.getOutputStream();

		StringBuffer sb = new StringBuffer();
		sb.append("<HTML><BODY>mobile: " + mobile + " </BODY></HTML>");
		os.write(sb.toString().getBytes());
		os.close();

	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

}
